package com.strawberry.chat2.library.netease.chatRoom;

import android.util.Log;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.chatroom.ChatRoomService;
import com.netease.nimlib.sdk.chatroom.model.EnterChatRoomData;
import com.netease.nimlib.sdk.chatroom.model.EnterChatRoomResultData;

public class ChatRoomModule extends ReactContextBaseJavaModule {
    private final static String TAG = "ChatRoomModule";

    public ChatRoomModule(ReactApplicationContext reactContext) {
        super(reactContext);
        Util.context = reactContext;
    }

    @Override
    public String getName() {
        return "H5NeteaseChatRoom";
    }

    @ReactMethod
    public void enterRoom(String roomId){
        // roomId 表示聊天室ID
        EnterChatRoomData data = new EnterChatRoomData(roomId);
        // 以登录一次不重试为例
        NIMClient.getService(ChatRoomService.class).enterChatRoomEx(data, 1).setCallback(new RequestCallback<EnterChatRoomResultData>() {
            @Override
            public void onSuccess(EnterChatRoomResultData result) {
                // 登录成功
                Log.d(TAG, "登录成功");
                Util.RegisterMsgListener();
            }

            @Override
            public void onFailed(int code) {
                // 登录失败
                Log.d(TAG, "登录失败");
            }

            @Override
            public void onException(Throwable exception) {
                // 错误
                Log.d(TAG, "登录错误");
            }
        });
    }

    @ReactMethod
    public void leaveRoom(String roomId){
        NIMClient.getService(ChatRoomService.class).exitChatRoom(roomId);
        Log.d(TAG, "离开聊天室");
        Util.RemoveMsgListener();
    }
}
